package com.love.slidingWindow;

public class MinWindow {

    public static String minWindow(String s, String t) {
        if (s.length() < t.length()) {
            return "";
        }
        char[] ss = s.toCharArray();
        char[] ts = t.toCharArray();
        int[] cns = new int[256];
        for (char c : ts) {
            cns[c]--;
        }
        int debt = t.length();
        int start = 0;
        int len = Integer.MAX_VALUE;
        for (int l = 0, r = 0; r < ss.length; r++) {
            // 这儿 无论 cns[ss[r]] 是否小于0，cns[ss[r]] 都会 ++
            if (cns[ss[r]]++ < 0) {
                debt--;
            }
            if (debt == 0) {
                while (cns[ss[l]] > 0) {
                    cns[ss[l++]]--;
                }
                if (r - l + 1 < len) {
                    len = r - l + 1;
                    start = l;
                }
            }

        }
        return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len);
    }


    public static void main(String[] args) {
        String s = "ADOBECODEBANC";
        String t = "ABC";

        minWindow(s,t);

    }



}